﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace FormView.UI.AlertView
{
	public class AlertFormWindow : FormWindowBase
	{
		// 点击窗体结果
		private AlertResult mAlertResult = AlertResult.ALERT_CANCEL;

		// 消息显示组件
		private TextBlock msgTextBlock;

		public AlertFormWindow(string @namespace) : base(@namespace)
		{
			Title = "消息提示";
			WindowStartupLocation = WindowStartupLocation.CenterScreen;
			Width = 300;
			Height = 200;
			Loaded += (sender, args) =>
			{
				if (msgTextBlock != null)
				{
					msgTextBlock.Height = ScrollContentHeight;
				}
			};
			SizeChanged += (sender, args) =>
			{
				if (msgTextBlock != null)
				{
					msgTextBlock.Height = ScrollContentHeight;
				}
			};
		}

		public void LoadView(string text, string positive, string negative, string neutral)
		{
			if (text == null)
			{
				throw new ArgumentException("AlertView的提示文字不可为空");
			}
			msgTextBlock = new TextBlock()
			{
				Text = text,
				FontSize = 14,
				Padding = new Thickness(10, 5, 10, 5),
				TextWrapping = TextWrapping.Wrap,
				VerticalAlignment = VerticalAlignment.Stretch,
				Background = DefaultBgColor,
				Foreground = DefaultForeColor,
				Height = ScrollContentHeight
			};
			AddControl(msgTextBlock);

			AlertFooterBar alertFooterBar = new AlertFooterBar(positive, negative, neutral);
			alertFooterBar.OnClickPositive += () => SelectResultAndCloseWindow(AlertResult.ALERT_POSITIVE);
			alertFooterBar.OnClickNegative += () => SelectResultAndCloseWindow(AlertResult.ALERT_NEGATIVE);
			alertFooterBar.OnClickNeutral += () => SelectResultAndCloseWindow(AlertResult.ALERT_NEUTRAL);
			SetFooterBar(alertFooterBar);
			SwitchFooterBarState(Visibility.Visible);
		}

		private void SelectResultAndCloseWindow(AlertResult res)
		{
			mAlertResult = res;
			Close();
		}

		protected override FormResult GetFormResult()
		{
			return new FormResult((int) mAlertResult);
		}

		protected override FormResult OnUserCloseWinGetResult()
		{
			return GetFormResult();
		}
	}
}
